#include <iostream>
#include <cstring>

using namespace std;

const int N = 2e5 + 10;

int a[N * 2];

int main()
{
	int t; cin >> t;
	while (t--)
	{
		memset(a, 0, sizeof a);
		int n, k; cin >> n >> k;
		int maxa = 0;
		for(int i = 1; i <= n; i++)
		{
			int x; cin >> x;
			a[x % (k * 2)]++;
			a[(x + k - 1) % (k * 2) + 1]--;
			maxa = max(maxa, x);
		} 
		for(int i = 1; i <= 2 * k; i++) a[i] += a[i - 1];
		int t = -1;
		for(int i = 0; i <= 2 * k; i++) 
		{
			if(a[i] == n)
			{
				t = i;
				break;
			}
		}
		if(t == -1) cout << -1 << endl;
		else cout << maxa + (t - maxa) % (2 * k) << endl;

	}
	return 0;
}
